package com.yanqu.road.server.manger.cricket.skillhandler.attround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.attround.CricketSkillAttRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill164Strategy implements CricketSkillAttRoundStrategy {

    /**
     * 技能处理-释放技能id{2}后,有{0}%概率下回合释放，每超出对方气势{1}%，千分比概率加10，最高加{3}%
     * @param cricketGame 本局对局对象
     * @param skillData 触发的技能
     * @param baseAttSkill 基础攻击技能
     * @param baseAttReHpResult 基础攻击结果对象
     * @param action 我方
     * @param target 对方
     * @param attackRoundBuilder 返回PB结构体
     * @return true：死亡，退出技能处理，false：继续技能处理
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, SkillData baseAttSkill, AddHpResult baseAttReHpResult, BattleData action, BattleData target, CricketProto.CricketAttackRound.Builder attackRoundBuilder, long battleFrameId) {
        //释放技能id{2}后,有{0}%概率下回合释放，每超出对方气势{1}%，千分比概率加10，最高加{3}%
        //处理成概率判定后重置{2}技能的冷却
        int checkSkillId = skillData.getSkillEffect().getP3();
        if (baseAttSkill.getSkillId() == checkSkillId) {
            int compareValue = skillData.getSkillEffect().getP1();
            if (action.getPower() > target.getPower()) {
                BigDecimal diffValue = BigDecimal.valueOf(action.getPower() - target.getPower());
                BigDecimal devideValue = BigDecimal.valueOf(Math.max(1, target.getPower()));
                BigDecimal p2Value = BigDecimal.valueOf(Math.max(1, skillData.getSkillEffect().getP2()));
                BigDecimal addDecimalValue = diffValue.multiply(BigDecimal.valueOf(10000)).
                        divide(devideValue, 8, RoundingMode.DOWN).
                        divide(p2Value, 8, RoundingMode.DOWN);
                int addValue = addDecimalValue.intValue() < skillData.getSkillEffect().getP4() ? addDecimalValue.intValue() : skillData.getSkillEffect().getP4();
                compareValue += addValue;
            }
            int randValue = cricketGame.rand1000();
            if (randValue < compareValue) {
                skillData.effectAmountInc();
                //判定成功,重置指定技能冷却
                action.resetSkillCoolingDown(checkSkillId);
            }
        }
        return false;
    }
}
